import Taro, { Component } from '@tarojs/taro'
import { View , Text , Image} from '@tarojs/components';
import { baseImgSrc } from '../../util/base_url';
import { AtButton } from 'taro-ui';
import { getPayInfo } from '../../util/api';
import './cashier.scss';
import { getRole } from '../../util/globaldata';
export default class Cashier extends Component {
    constructor(){
        super(...arguments);
        this.state = {
            model:"xia", //页面模式  xia 下单  jie 接单 
            payOrderInfo:{},
            order:"",
            money:"2",
            userRole:1,
        }
    }
    componentDidShow(){
        this.setState({
            userRole:getRole()
        })
    }
    componentWillMount(){
        let { model = 'xia' , order , money = '0.00' } = this.$router.params;
        this.setState({order,model,money});
    }
    payMoney(){
        let { order , model , userRole} = this.state;
        getPayInfo({order}).then( res => {
            console.log(res)
            if(res.code == 0){
                Taro.requestPayment({
                    timeStamp: res.data.timeStamp,
                    nonceStr:  res.data.nonceStr,
                    package:  res.data.package,
                    signType:  res.data.signType,
                    paySign:  res.data.paySign,
                    success: function (res) {
                        Taro.showModal({
                            title:"提示",
                            content:'支付成功',
                            success(res){
                                Taro.eventCenter.trigger("successPay");
                                Taro.switchTab({
                                    url:'/pages/tablist/tablist'
                                })
                            }
                        })
                     },
                    fail: function (res) {
                        Taro.showModal({
                            title:"提示",
                            content:'支付失败',
                        })
                    }
                })
            }else{
                Taro.showModal({
                    title:"提示",
                    content:res.msg
                })
            }
        })
    }
    render(){
        let { money } = this.state;
        return(
            <View className='cashier'>
                <View className='pay-card'>
                    <View className='should-pay flex align-items flex-start'>
                        <View className='left'>应付款:</View>
                        <View className='money flex1'>{money}</View>
                    </View>
                    <View className='pay-type'>
                        <View className='title flex flex-start align-items'>支付方式</View>
                        <View className='type-list flex flex-wrap flex-start'>
                            <View className='item fn-ctr'>
                                <Image className='item-img' src={`${baseImgSrc}/pay_icon.png`}/>
                                <Text className='item-txt'>微信支付</Text>
                            </View>
                        </View>
                    </View>
                </View>
                <AtButton className='next-btn' type='primary' size='normal' onClick={this.payMoney.bind(this)}>立即付款</AtButton>
            </View>
        )
    }
}